package cn.cmkj.auction.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.List;
import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Shuhua;
import cn.cmkj.auction.data.http.ShuhuaOrder;
import cn.cmkj.auction.ui.OrderDetailActivity;
import cn.cmkj.auction.utils.Utils;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class OrderListAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<ShuhuaOrder> mData;
    private ImageOptions imageOptions;

    public OrderListAdapter(Context context, List<ShuhuaOrder> mData) {
        this.context = context;
        this.mData = mData;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_shuhua_order, parent, false);
        return new OrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ShuhuaOrder item = mData.get(position);
        Shuhua shuhua = item.getShuhuaList().get(0);
        if(shuhua != null) {
            if(shuhua.getDefaultImage() != null) {
                x.image().bind(((OrderViewHolder) holder).image, shuhua.getDefaultImage().m_url, imageOptions);
            }else {
                ((OrderViewHolder) holder).image.setImageResource(R.drawable.empty_photo);
            }
            ((OrderViewHolder) holder).textName.setText(shuhua.getName());
        }
        ((OrderViewHolder) holder).textPrice.setText("￥" + item.getTotalShouldPay().doubleValue());
        //((OrderViewHolder) holder).textStatus.setBackgroundResource(R.drawable.order_status_red);
        ((OrderViewHolder) holder).textStatus.setVisibility(View.GONE);
        ((OrderViewHolder) holder).textTime.setText("日期：" + Utils.formatDate(item.getOrderTime()));
        ((OrderViewHolder) holder).textDisplayStatus.setText(item.getDisplayStatusText());
        if("active".equals(item.getOrderStatus()) && "0".equals(item.getPayStatusStr())) {
            ((OrderViewHolder) holder).goPay.setVisibility(View.VISIBLE);
            ((OrderViewHolder) holder).goPay.setText("去付款");
            ((OrderViewHolder) holder).goPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderDetailActivity.class);
                    intent.putExtra("order", item);
                    context.startActivity(intent);
                }
            });
        }else {
            ((OrderViewHolder) holder).goPay.setVisibility(View.GONE);
        }
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {
        public TextView textTime;
        public TextView textDisplayStatus;
        public ImageView image;
        public TextView textName;
        public TextView textPrice;
        public TextView textStatus;
        public Button goPay;

        public OrderViewHolder(View itemView) {
            super(itemView);
            this.textTime = (TextView) itemView.findViewById (R.id.order_time);
            this.textDisplayStatus = (TextView) itemView.findViewById (R.id.order_display_status);
            this.image = (ImageView) itemView.findViewById (R.id.order_item_image);
            this.textName = (TextView) itemView.findViewById (R.id.order_item_name);
            this.textPrice = (TextView) itemView.findViewById (R.id.text_need_pay_price);
            this.textStatus = (TextView) itemView.findViewById (R.id.order_item_status);
            this.goPay = (Button) itemView.findViewById (R.id.btn_go_pay);
        }
    }
}
